package base.tree;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

import app.util.StringUtil;

public class Tree {
	
	private Object children;
	
//	private TreeNode treeNode;
	
	private Map<String,Object> nodeMap;

//	public TreeNode getTreeNode() {
//		return treeNode;
//	}
//
//	public void setTreeNode(TreeNode treeNode) {
//		this.treeNode = treeNode;
//	}

	public Map<String, Object> getNodeMap() {
		return nodeMap;
	}


	public void setNodeMap(Map<String, Object> nodeMap) {
		this.nodeMap = nodeMap;
	}


	/**
	 * 建树
	 * @param dateList
	 * @param treeNodeNameKey
	 * @param treeParentKey
	 */
	public void buildTree(List<Map<String, Object>> dateList, String treeNodeNameKey, String treeParentKey){
//		List<TreeNode> treeNodeList = new ArrayList<TreeNode>();
		
		List<Map<String,Object>> nodeMapList = new ArrayList<Map<String,Object>>();
		
		
		/**
		 * 主要思路
		 * 在原来的数据源中添加 构建树的一些基本属性（parentId，leaf，children，text）
		 */
		for (int i = 0; dateList!=null && i < dateList.size(); i++) {
			Map<String, Object> map = dateList.get(i);
//			TreeNode treeNode = new TreeNode();
//			treeNode.setId(StringUtil.objectToStr(map.get("id")));
//			treeNode.setText(StringUtil.objectToStr(map.get(treeNodeNameKey)));
//			treeNode.setParentId(StringUtil.objectToStr(map.get(treeParentKey)));
//			treeNode.setLeaf(true);
//			treeNodeList.add(treeNode);
			
			map.put("leaf", true);
			map.put("text", StringUtil.objectToStr(map.get(treeNodeNameKey)));
			map.put("parentId", StringUtil.objectToStr(map.get(treeParentKey)));
			map.put("children", null);
			nodeMapList.add(map);
			
		}
		
		//原型
//		List<TreeNode> rootTrees = new ArrayList<TreeNode>();
//		
//        for (TreeNode treeNode : treeNodeList) {
//            if("root".equals(treeNode.getParentId())){
//            	rootTrees.add(treeNode);
//            }
//            for (TreeNode t : treeNodeList) {
//                if(t.getParentId().equals(treeNode.getId())){
//                    if(treeNode.getChildren() == null){
//                        List<TreeNode> myChildrens = new ArrayList<TreeNode>();
//                        myChildrens.add(t);
//                        treeNode.setLeaf(false);
//                        treeNode.setChildren(myChildrens);
//                    }else{
//                    	treeNode.setLeaf(false);
//                        treeNode.getChildren().add(t);
//                    }
//                }
//            }
//        }
        
		/**
		 * 双层循环
		 */
        List<Map<String, Object>> rootMapTrees = new ArrayList<Map<String, Object>>();
		
        for (Map<String, Object> treeNode : nodeMapList) {
            if("root".equals(treeNode.get("parentId"))){
            	rootMapTrees.add(treeNode);
            }
            for (Map<String, Object> t : nodeMapList) {
                if(t.get("parentId").equals(treeNode.get("id"))){
                    if(treeNode.get("children") == null){
                        List<Map<String, Object>> myChildrens = new ArrayList<Map<String, Object>>();
                        myChildrens.add(t);
                        treeNode.put("leaf", false);
                        treeNode.put("children", myChildrens);
                    }else{
                    	treeNode.put("leaf", false);
                    	((List<Map<String, Object>>)treeNode.get("children")).add(t);
                    }
                }
            }
        }
        this.setChildren(rootMapTrees);
	}

	
	public Object getChildren() {
		return children;
	}

	public void setChildren(Object children) {
		this.children = children;
	}

}
